;(function ($, window, document, undefined) {
    //定义goods的构造函数
    var goods = function (ele, opt) {
        this.$element = ele;
        this.defaults = {
            'inc': '.inc',
            'dec': '.dec',
            'num': '.num',
            'addcart':'.addcart',
            'addbuy':'.addbuy',
            "current":".current-price",
            "total":".total-price",
            "datadom":".choose-attrs"
        };
        this.options = $.extend({}, this.defaults, opt);
        this.options.numDom = $(this.$element).find(this.options.num);
        this.options.priceDom = $(this.$element).find(this.options.current);
        this.options.totalDom = $(this.$element).find(this.options.total);
    }
    goods.prototype = {
        num: function () {
            return parseInt(this.options.numDom.val());
        },
        getBase:function () {
            var _=this;
            return _.$element.find(_.options.datadom).data('base');
        },
        init: function () { //初始化方法
            var _ = this;
            if (isNaN(_.num())) {
                _.options.numDom.val(min);
                layer.msg('请输入数字！');
                return false;
            }
            //加
            $(_.$element).on('click', _.options.inc, function () {
                _.inc();
            })
            //减
            $(_.$element).on('click', _.options.dec, function () {
                _.dec();
            })
            //数值改变
            $(_.$element).on('change', _.options.num, function () {
                _.change();
            })
            //购买
            $(_.$element).on('click', _.options.addcart+','+_.options.addbuy, function () {
                var type='.'+$(this).attr('class');
                _.addcart(type);
            })
        },
        inc: function () { //增加
            var _ = this;
            var num = parseInt(_.num()) + parseInt(_.getBase().step);
            if (num > parseInt(_.getBase().stock)) {
                _.options.numDom.val(_.getBase().stock);
                layer.msg('库存不足！');
                return false;
            }
            _.options.numDom.val(num);
            _.change();
        },
        dec: function () { //减少
            var _ = this;
            var num = parseInt(_.num()) - parseInt(_.getBase().step);
            if (num < parseInt(_.getBase().min)) {
                _.options.numDom.val(_.getBase().min);
                layer.msg('起订量不能小于:' + _.getBase().min);
                return false;
            }
            _.options.numDom.val(num);
            _.change();
        },
        change: function () { //数值改变
            var _ = this;
            _.valid();
            var price = formatGoodsPrice(_.getBase().goods_price, _.num());
            _.options.priceDom.text(price.simplePrice);
            _.options.totalDom.text(price.totalPrice);
        },
        valid: function () { //验证
            var _ = this;
            if (isNaN(_.num())) {
                _.options.numDom.val(_.getBase().min);
                layer.msg('请输入数字！');
                return false;
            }
            if (_.num() > parseInt(_.getBase().stock)) {
                _.options.numDom.val(_.getBase().stock);
                layer.msg('库存不足！');
                return false;
            }
            if (_.num() < parseInt((_.getBase().min))) {
                _.options.numDom.val(_.getBase().min);
                layer.msg('起订量不能小于:' + _.getBase().min);
                return false;
            }
        },
        addcart: function (type) { //加入购物车、立即购买
            var _ = this;
            _.valid();
            var skuId = _.getBase().sku_id;
            var num = _.num();
            addCart(_.options.addurl, skuId, num, function (res) {
                if (res.code == 1) {
                    if (type == _.options.addcart) {
                        layer.msg(res.msg);
                    } else {
                        Urlhref(_.options.suborderurl);
                    }
                } else {
                    if(res.data=='nologin'){
                        location.href=res.url;
                    }else{
                        layer.msg(res.msg);
                    }
                }
            });
        }
    }
    $.fn.goodsPlugin = function (options) {
        var goodsPlugin = new goods(this, options);
        goodsPlugin.init();
        return goodsPlugin;
    }
})(jQuery, window, document);